﻿using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;
using System.Threading;


namespace CCPSIP
{
    public class PushKit
    {
        //private const string kPushServer = "192.168.52.80";//ios.ccp
        private const string kPushServer = "ios.ccp.com.tw";//ios.ccp
        private const int kPushPortCall = 9530;
        private const int kPushPortMsg = 9531;
        private const int kConnTimeout = 5000;//in ms
        private static byte[] kPushCmdBytes = Encoding.UTF8.GetBytes("PUSH");


        /// <summary>
        /// 通知 Push Service (new Thread)
        /// </summary>
        /// <param name="pushType">Push通知的類型</param>
        public static void WakePushAsync(PushType pushType)
        {
            WakePushParam paramObj = new WakePushParam(pushType);
            new Thread(new ParameterizedThreadStart(WakePush)).Start(paramObj);
        }


        /// <summary>
        /// 執行 Push Service 通知
        /// </summary>
        /// <param name="param">WakePushParam參數物件</param>
        private static void WakePush(object param)
        {
            try
            {
                if (param is WakePushParam)
                {
                    WakePushParam p = (WakePushParam)param;

                    //【01】Connect
                    TcpClientWithTimeout tClient = new TcpClientWithTimeout(p.PushServer, p.PushPort, kConnTimeout);
                    TcpClient clientSocket = tClient.Connect();
                    clientSocket.SendTimeout = 2000;
                    clientSocket.ReceiveTimeout = 2000;
                    clientSocket.ReceiveBufferSize = 1024;
                    NetworkStream socketStream = clientSocket.GetStream();

                    //【02】Send Command
                    socketStream.Write(kPushCmdBytes, 0, kPushCmdBytes.Length);
                    System.Threading.Thread.Sleep(500);//Sleep for Response

                    //【03】Read Response
                    byte[] inBuffer = new byte[clientSocket.ReceiveBufferSize];
                    socketStream.Read(inBuffer, 0, inBuffer.Length);

                    //【99】Clean
                    socketStream.Close();
                    clientSocket.Close();
                    socketStream = null;
                    clientSocket = null;
                }
            }
            catch (Exception ex)
            {
                ChrisKit.SkipVar(ex);
            }
        }


        /// <summary>
        /// Push Service 通知參數
        /// </summary>
        private class WakePushParam
        {
            private string _PushServer;
            private int _PushPort;

            public string PushServer { get { return this._PushServer; } }
            public int PushPort { get { return this._PushPort; } }


            public WakePushParam(PushType pushType)
            {
                _PushServer = kPushServer;

                if (pushType == PushType.Call)
                {
                    _PushPort = kPushPortCall;
                }
                else
                {
                    _PushPort = kPushPortMsg;
                }
            }
        }


        /// <summary>
        /// 把舊的 DevEnv 轉換成新的 PushType
        /// </summary>
        /// <param name="DevEnv">DevEnv開發類別代號</param>
        /// <param name="DeviceToken">Device Push Token</param>
        /// <returns>PushType推播類型</returns>
        public static string ToPushType(string DevEnv, string DeviceToken)
        {
            if (string.IsNullOrEmpty(DevEnv))
                return "N";

            DevEnv = DevEnv.ToUpper();

            if (DevEnv == "B")
                return "B";//百度雲推送
            if (DeviceToken.Length == 64)
            {
                if (DevEnv == "N")
                    return "IP";//iOS APNs: Production
                else
                    return "IS";//iOS APNs: Sandbox (Dev)
            }
            if (DeviceToken.Length > 160)
                return "A";//Android GCM

            return DevEnv;
        }

        public enum PushType
        {
            Call,
            Msg
        }
    }

}
